import { calculateRotatedPointCoordinate, getCenterPoint } from './translate'
//计算组件的尺寸大小和位置

const fun = {
    lt: calculateLeftTop,
    t: calculateTop,
    rt: calculateRightTop,
    r: calculateRight,
    rb: calculateRightBottom,
    b: calculateBottom,
    lb: calculateLeftBottom,
    l: calculateLeft,
}

// 左上
function calculateLeftTop(element, curPositon, proportion, needLockProportion, pointInfo) {
    const { symmetricPoint } = pointInfo
    let newCenterPoint = getCenterPoint(curPositon, symmetricPoint)
    let newTopLeftPoint = calculateRotatedPointCoordinate(curPositon, newCenterPoint, -element.r)
    let newBottomRightPoint = calculateRotatedPointCoordinate(symmetricPoint, newCenterPoint, -element.r)

    let newWidth = newBottomRightPoint.x - newTopLeftPoint.x
    let newHeight = newBottomRightPoint.y - newTopLeftPoint.y

    if (newWidth > 0 && newHeight > 0) {
        element.x = Math.round(newTopLeftPoint.x);
        element.y = Math.round(newTopLeftPoint.y);
        element.w = Math.round(newWidth);
        element.h = Math.round(newHeight);
    }
    return element
}
// 上
function calculateTop(element, curPositon, proportion, needLockProportion, pointInfo) { 
    const { symmetricPoint, curPoint } = pointInfo
    // 由于用户拉伸时是以任意角度拉伸的，所以在求得旋转前的坐标时，只取 y 坐标（这里的 x 坐标可能是任意值），x 坐标用 curPoint 的。
    // 这个中心点（第二个参数）用 curPoint, center, symmetricPoint 都可以，只要他们在一条直线上就行
    const rotatedcurPositon = calculateRotatedPointCoordinate(curPositon, curPoint, -element.r)

    // 算出旋转前 y 坐标，再用 curPoint 的 x 坐标，重新计算它们旋转后对应的坐标
    const rotatedTopMiddlePoint = calculateRotatedPointCoordinate({
        x: curPoint.x,
        y: rotatedcurPositon.y,
    }, curPoint, element.r)
    
    // 用旋转后的坐标和对称点算出新的高度（勾股定理）
    const newHeight = Math.sqrt((rotatedTopMiddlePoint.x - symmetricPoint.x) ** 2 + (rotatedTopMiddlePoint.y - symmetricPoint.y) ** 2)
    
    const newCenter = {
        x: rotatedTopMiddlePoint.x - (rotatedTopMiddlePoint.x - symmetricPoint.x) / 2,
        y: rotatedTopMiddlePoint.y + (symmetricPoint.y - rotatedTopMiddlePoint.y) / 2,
    }

    let width = element.w
    // 因为调整的是高度 所以只需根据锁定的比例调整宽度即可
    if (needLockProportion) {
        width = newHeight * proportion
    }
    
    element.w = width
    element.h = Math.round(newHeight)
    element.y = Math.round(newCenter.y - (newHeight / 2))
    element.x = Math.round(newCenter.x - (element.w / 2))
    return element 
}
// 右上
function calculateRightTop(element, curPositon, proportion, needLockProportion, pointInfo) {
    const { symmetricPoint } = pointInfo
    let newCenterPoint = getCenterPoint(curPositon, symmetricPoint)
    let newTopRightPoint = calculateRotatedPointCoordinate(curPositon, newCenterPoint, -element.r)
    let newBottomLeftPoint = calculateRotatedPointCoordinate(symmetricPoint, newCenterPoint, -element.r)

    let newWidth = newTopRightPoint.x - newBottomLeftPoint.x
    let newHeight = newBottomLeftPoint.y - newTopRightPoint.y

    if (newWidth > 0 && newHeight > 0) {
        element.w = Math.round(newWidth)
        element.h = Math.round(newHeight)
        element.x = Math.round(newBottomLeftPoint.x)
        element.y = Math.round(newTopRightPoint.y)
    }
    return element;
}
// 右
function calculateRight(element, curPositon, proportion, needLockProportion, pointInfo) { 
    const { symmetricPoint, curPoint } = pointInfo
    const rotatedcurPositon = calculateRotatedPointCoordinate(curPositon, curPoint, -element.r)
    const rotatedRightMiddlePoint = calculateRotatedPointCoordinate({
        x: rotatedcurPositon.x,
        y: curPoint.y,
    }, curPoint, element.r)
  
    const newWidth = Math.sqrt((rotatedRightMiddlePoint.x - symmetricPoint.x) ** 2 + (rotatedRightMiddlePoint.y - symmetricPoint.y) ** 2)

    const newCenter = {
        x: rotatedRightMiddlePoint.x - (rotatedRightMiddlePoint.x - symmetricPoint.x) / 2,
        y: rotatedRightMiddlePoint.y + (symmetricPoint.y - rotatedRightMiddlePoint.y) / 2,
    }

    let height = element.h
    // 因为调整的是宽度 所以只需根据锁定的比例调整高度即可
    if (needLockProportion) {
        height = newWidth / proportion
    }
        
    element.h = height
    element.w = Math.round(newWidth)
    element.y = Math.round(newCenter.y - (element.h / 2))
    element.x = Math.round(newCenter.x - (newWidth / 2))
    
    
    return element 
}
// 右下
function calculateRightBottom(element, curPositon, proportion, needLockProportion, pointInfo) {
    const { symmetricPoint } = pointInfo
    let newCenterPoint = getCenterPoint(curPositon, symmetricPoint)
    let newTopLeftPoint = calculateRotatedPointCoordinate(symmetricPoint, newCenterPoint, -element.r)
    let newBottomRightPoint = calculateRotatedPointCoordinate(curPositon, newCenterPoint, -element.r)

    let newWidth = newBottomRightPoint.x - newTopLeftPoint.x
    let newHeight = newBottomRightPoint.y - newTopLeftPoint.y

    if (newWidth > 0 && newHeight > 0) {
        element.w = Math.round(newWidth)
        element.h = Math.round(newHeight)
        element.x = Math.round(newTopLeftPoint.x) 
        element.y = Math.round(newTopLeftPoint.y)
    }
    return element;
}
// 下
function calculateBottom(element, curPositon, proportion, needLockProportion, pointInfo) { 
    const { symmetricPoint, curPoint } = pointInfo
    const rotatedcurPositon = calculateRotatedPointCoordinate(curPositon, curPoint, -element.r)
    const rotatedBottomMiddlePoint = calculateRotatedPointCoordinate({
        x: curPoint.x,
        y: rotatedcurPositon.y,
    }, curPoint, element.r)
  
    const newHeight = Math.sqrt((rotatedBottomMiddlePoint.x - symmetricPoint.x) ** 2 + (rotatedBottomMiddlePoint.y - symmetricPoint.y) ** 2)

    const newCenter = {
        x: rotatedBottomMiddlePoint.x - (rotatedBottomMiddlePoint.x - symmetricPoint.x) / 2,
        y: rotatedBottomMiddlePoint.y + (symmetricPoint.y - rotatedBottomMiddlePoint.y) / 2,
    }

    let width = element.w
    // 因为调整的是高度 所以只需根据锁定的比例调整宽度即可
    if (needLockProportion) {
        width = newHeight * proportion
    }
        
    element.w = width
    element.h = Math.round(newHeight)
    element.y = Math.round(newCenter.y - (newHeight / 2))
    element.x = Math.round(newCenter.x - (element.w / 2))
    
    
    return element 
}
// 左下
function calculateLeftBottom(element, curPositon, proportion, needLockProportion, pointInfo) { 
    const { symmetricPoint } = pointInfo
    let newCenterPoint = getCenterPoint(curPositon, symmetricPoint)
    let newTopRightPoint = calculateRotatedPointCoordinate(symmetricPoint, newCenterPoint, -element.r)
    let newBottomLeftPoint = calculateRotatedPointCoordinate(curPositon, newCenterPoint, -element.r)

    let newWidth = newTopRightPoint.x - newBottomLeftPoint.x
    let newHeight = newBottomLeftPoint.y - newTopRightPoint.y

    if (needLockProportion) {
        if (newWidth / newHeight > proportion) {
            newBottomLeftPoint.x += Math.abs(newWidth - newHeight * proportion)
            newWidth = newHeight * proportion
        } else {
            newBottomLeftPoint.y -= Math.abs(newHeight - newWidth / proportion)
            newHeight = newWidth / proportion
        }

        const rotatedBottomLeftPoint = calculateRotatedPointCoordinate(newBottomLeftPoint, newCenterPoint, element.r)
        newCenterPoint = getCenterPoint(rotatedBottomLeftPoint, symmetricPoint)
        newTopRightPoint = calculateRotatedPointCoordinate(symmetricPoint, newCenterPoint, -element.r)
        newBottomLeftPoint = calculateRotatedPointCoordinate(rotatedBottomLeftPoint, newCenterPoint, -element.r)

        newWidth = newTopRightPoint.x - newBottomLeftPoint.x
        newHeight = newBottomLeftPoint.y - newTopRightPoint.y
    }

    if (newWidth > 0 && newHeight > 0) {
        element.w = Math.round(newWidth)
        element.h = Math.round(newHeight)
        element.x = Math.round(newBottomLeftPoint.x)
        element.y = Math.round(newTopRightPoint.y)
    }
    
    return element 
}
// 左
function calculateLeft(element, curPositon, proportion, needLockProportion, pointInfo) { 
    const { symmetricPoint, curPoint } = pointInfo
    const rotatedcurPositon = calculateRotatedPointCoordinate(curPositon, curPoint, -element.r)
    const rotatedLeftMiddlePoint = calculateRotatedPointCoordinate({
        x: rotatedcurPositon.x,
        y: curPoint.y,
    }, curPoint, element.r)
  
    const newWidth = Math.sqrt((rotatedLeftMiddlePoint.x - symmetricPoint.x) ** 2 + (rotatedLeftMiddlePoint.y - symmetricPoint.y) ** 2)

    const newCenter = {
        x: rotatedLeftMiddlePoint.x - (rotatedLeftMiddlePoint.x - symmetricPoint.x) / 2,
        y: rotatedLeftMiddlePoint.y + (symmetricPoint.y - rotatedLeftMiddlePoint.y) / 2,
    }
        
    let height = element.h
    if (needLockProportion) {
        height = newWidth / proportion
    }
        
    element.h = height
    element.w = Math.round(newWidth)
    element.y = Math.round(newCenter.y - (element.h / 2))
    element.x = Math.round(newCenter.x - (newWidth / 2))
    return element 
}

export default function calculateComponentPositonAndSize(name, element, curPositon, proportion, needLockProportion, pointInfo) {
    return fun[name](element, curPositon, proportion, needLockProportion, pointInfo)
}
